<template>
  <el-container style="height: 100vh;">
    <!-- 左侧导航栏 -->
    <el-aside width="200px">
      <el-menu
        default-active="2"
        class="el-menu-vertical-demo"
        @open="handleOpen"
        @close="handleClose"
        background-color="#545c64"
        text-color="#fff"
        active-text-color="#ffd04b"
        router
      >
        <!-- 首页菜单 -->
        <el-menu-item index="/">
          <i class="el-icon-house"></i>
          <span slot="title">首页</span>
        </el-menu-item>

        <!-- 宿舍管理子菜单 -->
        <el-submenu index="1">
          <template slot="title">
            <i class="el-icon-location"></i>
            <span>宿舍管理</span>
          </template>
          <el-menu-item index="/students">学生管理</el-menu-item>
          <el-menu-item index="/dormitories">宿舍信息</el-menu-item>
          <el-menu-item index="/allocation">宿舍分配</el-menu-item>
          <el-menu-item index="/fees">水电费管理</el-menu-item>
        </el-submenu>

        <!-- 数据分析菜单 -->
        <el-submenu index="2">
          <template slot="title">
            <i class="el-icon-data-analysis"></i>
            <span>数据分析</span>
          </template>
          <el-menu-item index="/charts">数据可视化</el-menu-item>
        </el-submenu>

        <!-- 关于系统菜单 -->
        <el-menu-item index="/about">
          <i class="el-icon-info"></i>
          <span slot="title">关于系统</span>
        </el-menu-item>
      </el-menu>
    </el-aside>

    <!-- 右侧内容区域 -->
    <el-container> 
  <!-- 顶部标题 + 退出按钮 -->
  <el-header style="cursor: pointer;" @click="$router.push('/')">
    <div style="display: flex; justify-content: space-between; align-items: center;">
      <h2 style="margin: 0;">宿舍管理系统</h2>
      
      <!-- 只有未登录时显示“刷新可登出” -->
      <p v-if="!isLoggedIn">刷新可登出</p>

      <!-- 登录后显示欢迎信息和退出按钮 -->
      <div v-if="isLoggedIn">
        <span style="margin-right: 10px; color: #333;">欢迎，admin</span>
        <el-button type="text" @click.stop="logout">退出登录</el-button>
      </div>
    </div>
  </el-header>

      <!-- 主体内容 -->
      <el-main>
        <router-view />
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      isLoggedIn: false
    }
  },
  created() {
    this.checkLoginStatus()
  },
  methods: {
    handleOpen(key, keyPath) {
      console.log('菜单打开:', key, keyPath)
    },
    handleClose(key, keyPath) {
      console.log('菜单关闭:', key, keyPath)
    },
    checkLoginStatus() {
      const loggedIn = localStorage.getItem('isLoggedIn')
      if (loggedIn === 'true') {
        this.isLoggedIn = true
      } else {
        // 首次进入页面时不显示退出按钮
        this.isLoggedIn = false
      }
    },
    logout() {
      localStorage.removeItem('isLoggedIn') // 清除登录状态
      this.isLoggedIn = false // 更新状态
      this.$router.push('/login') // 跳转到登录页
    }
  }
}
</script>

<style>
.el-aside {
  background-color: #545c64;
}

.el-header {
  background-color: #f5f7fa;
  padding: 15px;
  text-align: left;
  font-size: 20px;
  border-bottom: 1px solid #e4e7ed;
  transition: background-color 0.3s;
}

.el-header:hover {
  background-color: #e9eef3;
}

.el-menu-vertical-demo:not(.el-menu--collapse) {
  width: 100%;
  min-height: 400px;
}
</style>